def run_algorithm(outlets, user, green, power)
	   data = []
	   preference = user.last.consumption_preference	  
	   util_cntrl_lvl = user.last.utility_control_lvl
	   OR_status = user.last.OR_status
	   #green power factor (GPF)
	   #GPF = (GreenSimulation.where(["logged_at like '%?%'",Time.now.hour]).last/PowerLog.last.power_consumption)
       green_percent = green / power
     if OR_status == true  
         case util_cntrl_lvl
         when util_cntrl_lvl = 1
		   for outlet in outlets
			case preference
			when preference = 1
			 if outlet.priority != 1 and outlet.status == true
				data << outlet.outlet_id
				o = OutletStatus.find(outlet.id)
				o.update_attribute(:status, 0)
			 elsif outlet.priority == 1 and outlet.status == false
				data << outlet.outlet_id
				o = OutletStatus.find(outlet.id)
				o.update_attribute(:status, 1)
			 end
			when preference = 2
			  if green_percent >= 0.8 and outlet.priority < 3 and outlet.status == false
				data << outlet.outlet_id
				o = OutletStatus.find(outlet.id)
				o.update_attribute(:status, 1)
			  elsif green_percent >= 1 and outlet.priority == 3 and outlet.status == false
				data << outlet.outlet_id
				o = OutletStatus.find(outlet.id)
				o.update_attribute(:status, 1)
			  elsif green_percent < 0.8 and outlet.priority == 2 and outlet.status == true
				data << outlet.outlet_id
				o = OutletStatus.find(outlet.id)
				o.update_attribute(:status, 0)
			  elsif green_percent < 1 and outlet.priority == 3 and outlet.status == true
				data << outlet.outlet_id
				o = OutletStatus.find(outlet.id)
				o.update_attribute(:status, 0)
			  end
			when preference = 3
			  if green_percent >= 0.4 and outlet.priority < 3 and outlet.status == false
				data << outlet.outlet_id
				o = OutletStatus.find(outlet.id)
				o.update_attribute(:status, 1)
			  elsif green_percent >= 0.6 and outlet.priority == 3 and outlet.status == false
				data << outlet.outlet_id
				o = OutletStatus.find(outlet.id)
				o.update_attribute(:status, 1)
			  elsif green_percent < 0.4 and outlet.priority == 2 and outlet.status == true
				data << outlet.outlet_id
				o = OutletStatus.find(outlet.id)
				o.update_attribute(:status, 0)
			  elsif green_percent < 0.6 and outlet.priority == 3 and outlet.status == true
				data << outlet.outlet_id
				o = OutletStatus.find(outlet.id)
				o.update_attribute(:status, 0)
			  end
			when preference = 4
			  if green_percent >= 0.1 and outlet.priority < 3 and outlet.status == false
				data << outlet.outlet_id
				o = OutletStatus.find(outlet.id)
				o.update_attribute(:status, 1)
			  elsif green_percent >= 0.3 and outlet.priority == 3 and outlet.status == false
				data << outlet.outlet_id
				o = OutletStatus.find(outlet.id)
				o.update_attribute(:status, 1)
			  elsif green_percent < 0.1 and outlet.priority == 2 and outlet.status == true
				data << outlet.outlet_id
				o = OutletStatus.find(outlet.id)
				o.update_attribute(:status, 0)
			  elsif green_percent < 0.3 and outlet.priority == 3 and outlet.status == true
				data << outlet.outlet_id
				o = OutletStatus.find(outlet.id)
				o.update_attribute(:status, 0)
			  end
			when preference = 5
				if outlet.status == false
				  data << outlet.outlet_id
				  o = OutletStatus.find(outlet.id)
				o.update_attribute(:status, 1)
				end
			end
	   end
	   	 when util_cntrl_lvl = 2
	   	   #turn off priority 3
	   	   for outlet in outlets
			case preference
			when preference = 1
			 if outlet.priority != 1 and outlet.status == true
				data << outlet.outlet_id
				o = OutletStatus.find(outlet.id)
				o.update_attribute(:status, 0)
			 elsif outlet.priority == 1 and outlet.status == false
				data << outlet.outlet_id
				o = OutletStatus.find(outlet.id)
				o.update_attribute(:status, 1)
			 end
			when preference = 2
			  if green_percent >= 0.8 and outlet.priority < 3 and outlet.status == false
				data << outlet.outlet_id
				o = OutletStatus.find(outlet.id)
				o.update_attribute(:status, 1)
			  elsif green_percent < 0.8 and outlet.priority == 2 and outlet.status == true
				data << outlet.outlet_id
				o = OutletStatus.find(outlet.id)
				o.update_attribute(:status, 0)
			  elsif outlet.priority == 3 and outlet.status == true
				data << outlet.outlet_id
				o = OutletStatus.find(outlet.id)
				o.update_attribute(:status, 0)
			  end
			when preference = 3
			  if green_percent >= 0.4 and outlet.priority < 3 and outlet.status == false
				data << outlet.outlet_id
				o = OutletStatus.find(outlet.id)
				o.update_attribute(:status, 1)
			  elsif green_percent < 0.4 and outlet.priority == 2 and outlet.status == true
				data << outlet.outlet_id
				o = OutletStatus.find(outlet.id)
				o.update_attribute(:status, 0)
			  elsif outlet.priority == 3 and outlet.status == true
				data << outlet.outlet_id
				o = OutletStatus.find(outlet.id)
				o.update_attribute(:status, 0)
			  end
			when preference = 4
			  if green_percent >= 0.1 and outlet.priority < 3 and outlet.status == false
				data << outlet.outlet_id
				o = OutletStatus.find(outlet.id)
				o.update_attribute(:status, 1)
			  elsif green_percent < 0.1 and outlet.priority == 2 and outlet.status == true
				data << outlet.outlet_id
				o = OutletStatus.find(outlet.id)
				o.update_attribute(:status, 0)
			  elsif outlet.priority == 3 and outlet.status == true
				data << outlet.outlet_id
				o = OutletStatus.find(outlet.id)
				o.update_attribute(:status, 0)
			  end
			when preference = 5
				if outlet.priority > 3 AND outlet.status == false
				  data << outlet.outlet_id
				  o = OutletStatus.find(outlet.id)
				  o.update_attribute(:status, 1)
				elsif outlet.priority == 3 and outlet.status == true
				  data << outlet.outlet_id
				  o = OutletStatus.find(outlet.id)
				  o.update_attribute(:status, 0)
				end
			end
	   end #turns off priority 3
	   	 when util_cntrl_lvl = 3 
	   	   if override_lvl == 3
	   	   	#turn off priority 3
	   	   	for outlet in outlets
			case preference
			when preference = 1
			 if outlet.priority != 1 and outlet.status == true
				data << outlet.outlet_id
				o = OutletStatus.find(outlet.id)
				o.update_attribute(:status, 0)
			 elsif outlet.priority == 1 and outlet.status == false
				data << outlet.outlet_id
				o = OutletStatus.find(outlet.id)
				o.update_attribute(:status, 1)
			 end
			when preference = 2
			  if green_percent >= 0.8 and outlet.priority < 3 and outlet.status == false
				data << outlet.outlet_id
				o = OutletStatus.find(outlet.id)
				o.update_attribute(:status, 1)
			  elsif green_percent < 0.8 and outlet.priority == 2 and outlet.status == true
				data << outlet.outlet_id
				o = OutletStatus.find(outlet.id)
				o.update_attribute(:status, 0)
			  elsif outlet.priority == 3 and outlet.status == true
				data << outlet.outlet_id
				o = OutletStatus.find(outlet.id)
				o.update_attribute(:status, 0)
			  end
			when preference = 3
			  if green_percent >= 0.4 and outlet.priority < 3 and outlet.status == false
				data << outlet.outlet_id
				o = OutletStatus.find(outlet.id)
				o.update_attribute(:status, 1)
			  elsif green_percent < 0.4 and outlet.priority == 2 and outlet.status == true
				data << outlet.outlet_id
				o = OutletStatus.find(outlet.id)
				o.update_attribute(:status, 0)
			  elsif outlet.priority == 3 and outlet.status == true
				data << outlet.outlet_id
				o = OutletStatus.find(outlet.id)
				o.update_attribute(:status, 0)
			  end
			when preference = 4
			  if green_percent >= 0.1 and outlet.priority < 3 and outlet.status == false
				data << outlet.outlet_id
				o = OutletStatus.find(outlet.id)
				o.update_attribute(:status, 1)
			  elsif green_percent < 0.1 and outlet.priority == 2 and outlet.status == true
				data << outlet.outlet_id
				o = OutletStatus.find(outlet.id)
				o.update_attribute(:status, 0)
			  elsif outlet.priority == 3 and outlet.status == true
				data << outlet.outlet_id
				o = OutletStatus.find(outlet.id)
				o.update_attribute(:status, 0)
			  end
			when preference = 5
				if outlet.priority > 3 AND outlet.status == false
				  data << outlet.outlet_id
				  o = OutletStatus.find(outlet.id)
				  o.update_attribute(:status, 1)
				elsif outlet.priority == 3 and outlet.status == true
				  data << outlet.outlet_id
				  o = OutletStatus.find(outlet.id)
				  o.update_attribute(:status, 0)
				end
			end
	   end #turns off priority 3
	   	   elsif override_lvl == 2 OR override_lvl == 1
	   	    #turn off priority 2 & 3
	   	    for outlet in outlets
			if outlet.priority != 1 and outlet.status == true
				data << outlet.outlet_id
				o = OutletStatus.find(outlet.id)
				o.update_attribute(:status, 0)
			 elsif outlet.priority == 1 and outlet.status == false
				data << outlet.outlet_id
				o = OutletStatus.find(outlet.id)
				o.update_attribute(:status, 1)
			 end
	   end #turns off priority 2 & 3
	   	   end
	   	 when util_cntrl_lvl = 4
	   	   if override_lvl == 3
	   	   	#turn off priority 3
	   	   	for outlet in outlets
			case preference
			when preference = 1
			 if outlet.priority != 1 and outlet.status == true
				data << outlet.outlet_id
				o = OutletStatus.find(outlet.id)
				o.update_attribute(:status, 0)
			 elsif outlet.priority == 1 and outlet.status == false
				data << outlet.outlet_id
				o = OutletStatus.find(outlet.id)
				o.update_attribute(:status, 1)
			 end
			when preference = 2
			  if green_percent >= 0.8 and outlet.priority < 3 and outlet.status == false
				data << outlet.outlet_id
				o = OutletStatus.find(outlet.id)
				o.update_attribute(:status, 1)
			  elsif green_percent < 0.8 and outlet.priority == 2 and outlet.status == true
				data << outlet.outlet_id
				o = OutletStatus.find(outlet.id)
				o.update_attribute(:status, 0)
			  elsif outlet.priority == 3 and outlet.status == true
				data << outlet.outlet_id
				o = OutletStatus.find(outlet.id)
				o.update_attribute(:status, 0)
			  end
			when preference = 3
			  if green_percent >= 0.4 and outlet.priority < 3 and outlet.status == false
				data << outlet.outlet_id
				o = OutletStatus.find(outlet.id)
				o.update_attribute(:status, 1)
			  elsif green_percent < 0.4 and outlet.priority == 2 and outlet.status == true
				data << outlet.outlet_id
				o = OutletStatus.find(outlet.id)
				o.update_attribute(:status, 0)
			  elsif outlet.priority == 3 and outlet.status == true
				data << outlet.outlet_id
				o = OutletStatus.find(outlet.id)
				o.update_attribute(:status, 0)
			  end
			when preference = 4
			  if green_percent >= 0.1 and outlet.priority < 3 and outlet.status == false
				data << outlet.outlet_id
				o = OutletStatus.find(outlet.id)
				o.update_attribute(:status, 1)
			  elsif green_percent < 0.1 and outlet.priority == 2 and outlet.status == true
				data << outlet.outlet_id
				o = OutletStatus.find(outlet.id)
				o.update_attribute(:status, 0)
			  elsif outlet.priority == 3 and outlet.status == true
				data << outlet.outlet_id
				o = OutletStatus.find(outlet.id)
				o.update_attribute(:status, 0)
			  end
			when preference = 5
				if outlet.priority > 3 AND outlet.status == false
				  data << outlet.outlet_id
				  o = OutletStatus.find(outlet.id)
				  o.update_attribute(:status, 1)
				elsif outlet.priority == 3 and outlet.status == true
				  data << outlet.outlet_id
				  o = OutletStatus.find(outlet.id)
				  o.update_attribute(:status, 0)
				end
			end
	   end #turns off priority 3
	   	   elsif override_lvl == 2
	   	    #turn off priority 2 & 3
	   	    for outlet in outlets
			if outlet.priority != 1 and outlet.status == true
				data << outlet.outlet_id
				o = OutletStatus.find(outlet.id)
				o.update_attribute(:status, 0)
			 elsif outlet.priority == 1 and outlet.status == false
				data << outlet.outlet_id
				o = OutletStatus.find(outlet.id)
				o.update_attribute(:status, 1)
			 end
	   end #turns off priority 2 & 3
	   	   elsif override_lvl == 1
	   	    #turn off all priorities
	   	    for outlet in outlets
			if outlet.status == true
				data << outlet.outlet_id
				o = OutletStatus.find(outlet.id)
				o.update_attribute(:status, 0)
			end
	    end 
	   	   end
	   	 end
	else
	   	for outlet in outlets
			case preference
			when preference = 1
			 if outlet.priority != 1 and outlet.status == true
				data << outlet.outlet_id
				o = OutletStatus.find(outlet.id)
				o.update_attribute(:status, 0)
			 elsif outlet.priority == 1 and outlet.status == false
				data << outlet.outlet_id
				o = OutletStatus.find(outlet.id)
				o.update_attribute(:status, 1)
			 end
			when preference = 2
			  if green_percent >= 0.8 and outlet.priority < 3 and outlet.status == false
				data << outlet.outlet_id
				o = OutletStatus.find(outlet.id)
				o.update_attribute(:status, 1)
			  elsif green_percent >= 1 and outlet.priority == 3 and outlet.status == false
				data << outlet.outlet_id
				o = OutletStatus.find(outlet.id)
				o.update_attribute(:status, 1)
			  elsif green_percent < 0.8 and outlet.priority == 2 and outlet.status == true
				data << outlet.outlet_id
				o = OutletStatus.find(outlet.id)
				o.update_attribute(:status, 0)
			  elsif green_percent < 1 and outlet.priority == 3 and outlet.status == true
				data << outlet.outlet_id
				o = OutletStatus.find(outlet.id)
				o.update_attribute(:status, 0)
			  end
			when preference = 3
			  if green_percent >= 0.4 and outlet.priority < 3 and outlet.status == false
				data << outlet.outlet_id
				o = OutletStatus.find(outlet.id)
				o.update_attribute(:status, 1)
			  elsif green_percent >= 0.6 and outlet.priority == 3 and outlet.status == false
				data << outlet.outlet_id
				o = OutletStatus.find(outlet.id)
				o.update_attribute(:status, 1)
			  elsif green_percent < 0.4 and outlet.priority == 2 and outlet.status == true
				data << outlet.outlet_id
				o = OutletStatus.find(outlet.id)
				o.update_attribute(:status, 0)
			  elsif green_percent < 0.6 and outlet.priority == 3 and outlet.status == true
				data << outlet.outlet_id
				o = OutletStatus.find(outlet.id)
				o.update_attribute(:status, 0)
			  end
			when preference = 4
			  if green_percent >= 0.1 and outlet.priority < 3 and outlet.status == false
				data << outlet.outlet_id
				o = OutletStatus.find(outlet.id)
				o.update_attribute(:status, 1)
			  elsif green_percent >= 0.3 and outlet.priority == 3 and outlet.status == false
				data << outlet.outlet_id
				o = OutletStatus.find(outlet.id)
				o.update_attribute(:status, 1)
			  elsif green_percent < 0.1 and outlet.priority == 2 and outlet.status == true
				data << outlet.outlet_id
				o = OutletStatus.find(outlet.id)
				o.update_attribute(:status, 0)
			  elsif green_percent < 0.3 and outlet.priority == 3 and outlet.status == true
				data << outlet.outlet_id
				o = OutletStatus.find(outlet.id)
				o.update_attribute(:status, 0)
			  end
			when preference = 5
				if outlet.status == false
				  data << outlet.outlet_id
				  o = OutletStatus.find(outlet.id)
				o.update_attribute(:status, 1)
				end
			end
	   end
	    return data
	end
